﻿using System;
using System.Collections.Generic;
using System.Web.UI;
using MS.InformationManagement.DAL.DocumentCenter;
using System.Linq;
using Microsoft.SharePoint;
using MS.InformationManagement.Support;

namespace MS.InformationManagement.Controls._CONTROLTEMPLATES.InformationManagerControls
{
    public partial class DocumentTraceabilityControl : UserControl
    {
        #region [Private Fields]

        private IEnumerable<TraceabilityEntry> _DocumentTraceEntries; 

        #endregion
        
        #region [Public Properties]

        public IEnumerable<TraceabilityEntry> DocumentTraceEntries
        {
            get { return Cache[SPContext.Current.Web.CurrentUser.LoginName.GetUserWithOutDomain()+"DocumentTraceEntries"] as IEnumerable<TraceabilityEntry>; }
            set
            {
                Cache[SPContext.Current.Web.CurrentUser.LoginName.GetUserWithOutDomain() + "DocumentTraceEntries"] = value;
                Pager1.PageIndex = 0;
                Pager1.TotalRecords = value.Count();
                gvLog.DataSource = value.Take(Pager1.PageSize);
                gvLog.DataBind();
                Pager1.BuildPager();
            }
        }
 
        #endregion

        #region [Event Handlers]

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            Pager1.OnPageChanged += new Pager.PageChangeHandler(Pager1_OnPageChanged);
            //Pager1.NextLinkText = "Siguiente";
            //Pager1.PreviousLinkText = "Anterior";
            Pager1.NumberOfPagesToDisplay = 10;
            Pager1.PageSize = 5;
            Pager1.PageIndex = 0;

        }

        protected void Pager1_OnPageChanged(object sender, PageChangeEventArgs e)
        {
            int startingIndex = Pager1.PageIndex * Pager1.PageSize;
            gvLog.DataSource = DocumentTraceEntries.Skip(startingIndex).Take(Pager1.PageSize);
            gvLog.DataBind();
            Pager1.TotalRecords = DocumentTraceEntries.Count();
            Pager1.BuildPager();
        }
 
        #endregion

        //public IEnumerable<TraceabilityEntry> DocumentTraceEntries
        //{
        //    get { return gvLog.DataSource as IEnumerable<TraceabilityEntry>; }
        //    set
        //    {
        //        gvLog.DataSource = value;
        //        gvLog.DataBind();
        //    }
        //}
    }
}
